home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / NetHack 3.1.3 / source / sys / share / dgn_yacc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  30.2 KB  |  1,060 lines  |  [TEXT/R*ch]

  1. #ifndef lint
  2. static char yysccsid[] = "@(#)yaccpar    1.9 (Berkeley) 02/21/93";
  3. #endif
  4. #define YYBYACC 1
  5. #define YYMAJOR 1
  6. #define YYMINOR 9
  7. #define yyclearin (yychar=(-1))
  8. #define yyerrok (yyerrflag=0)
  9. #define YYRECOVERING (yyerrflag!=0)
  10. #define YYPREFIX "yy"
  11. /*    SCCS Id: @(#)dgn_comp.c    3.1    93/05/15    */
  12. /*    Copyright (c) 1989 by Jean-Christophe Collet */
  13. /*    Copyright (c) 1990 by M. Stephenson                  */
  14. /* NetHack may be freely redistributed.  See license for details. */
  15.  
  16. /*
  17.  * This file contains the Dungeon Compiler code
  18.  */
  19.  
  20. /* In case we're using bison in AIX.  This definition must be
  21.  * placed before any other C-language construct in the file
  22.  * excluding comments and preprocessor directives (thanks IBM
  23.  * for this wonderful feature...).
  24.  *
  25.  * Note: some cpps barf on this 'undefined control' (#pragma).
  26.  * Addition of the leading space seems to prevent barfage for now,
  27.  * and AIX will still see the directive in its non-standard locale.
  28.  */
  29.  
  30. #ifdef _AIX
  31.  #pragma alloca        /* keep leading space! */
  32. #endif
  33.  
  34. #include "config.h"
  35. #include "dgn_file.h"
  36.  
  37. void FDECL(yyerror, (const char *));
  38. void FDECL(yywarning, (const char *));
  39. int NDECL(yylex);
  40. int NDECL(yyparse);
  41. int FDECL(getchain, (char *));
  42. int NDECL(check_dungeon);
  43. int NDECL(check_branch);
  44. int NDECL(check_level);
  45. void NDECL(init_dungeon);
  46. void NDECL(init_branch);
  47. void NDECL(init_level);
  48. void NDECL(output_dgn);
  49.  
  50. #ifdef AMIGA
  51. # undef    printf
  52. #ifndef    LATTICE
  53. # define    memset(addr,val,len)    setmem(addr,len,val)
  54. #endif
  55. #endif
  56.  
  57. #ifdef MICRO
  58. # undef exit
  59. extern void FDECL(exit, (int));
  60. #endif
  61.  
  62. #undef NULL
  63.  
  64. #define ERR        (-1)
  65.  
  66. static struct couple couple;
  67. static struct tmpdungeon tmpdungeon[MAXDUNGEON];
  68. static struct tmplevel tmplevel[LEV_LIMIT];
  69. static struct tmpbranch tmpbranch[BRANCH_LIMIT];
  70.  
  71. static int in_dungeon = 0, n_dgns = -1, n_levs = -1, n_brs = -1;
  72.  
  73. extern int fatal_error;
  74. extern const char *fname;
  75.  
  76. typedef union
  77. {
  78.     int    i;
  79.     char*    str;
  80. } YYSTYPE;
  81. #define INTEGER 257
  82. #define A_DUNGEON 258
  83. #define BRANCH 259
  84. #define CHBRANCH 260
  85. #define LEVEL 261
  86. #define RNDLEVEL 262
  87. #define CHLEVEL 263
  88. #define RNDCHLEVEL 264
  89. #define UP_OR_DOWN 265
  90. #define PROTOFILE 266
  91. #define DESCRIPTION 267
  92. #define DESCRIPTOR 268
  93. #define LEVELDESC 269
  94. #define ALIGNMENT 270
  95. #define LEVALIGN 271
  96. #define ENTRY 272
  97. #define STAIR 273
  98. #define NO_UP 274
  99. #define NO_DOWN 275
  100. #define PORTAL 276
  101. #define STRING 277
  102. #define YYERRCODE 256
  103. short yylhs[] = {                                        -1,
  104.     0,    0,    4,    4,    5,    5,    5,    5,    6,    1,
  105.     1,    7,    7,    7,   11,   12,   14,   14,   13,    9,
  106.     9,    9,    9,    9,   15,   15,   16,   16,   17,   17,
  107.    18,   18,   19,   19,    8,    8,   21,   22,    3,    3,
  108.     3,    3,    3,    2,    2,   20,   10,
  109. };
  110. short yylen[] = {                                         2,
  111.     0,    1,    1,    2,    1,    1,    1,    1,    6,    0,
  112.     1,    1,    1,    1,    3,    1,    3,    3,    3,    1,
  113.     1,    1,    1,    1,    6,    7,    7,    8,    3,    3,
  114.     7,    8,    8,    9,    1,    1,    7,    8,    0,    1,
  115.     1,    1,    1,    0,    1,    5,    5,
  116. };
  117. short yydefred[] = {                                      0,
  118.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  119.     0,    0,    0,    0,    0,    3,    5,    6,    7,    8,
  120.    12,   13,   14,   16,   20,   21,   22,   23,   24,   35,
  121.    36,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  122.     0,    0,    0,    0,    4,    0,    0,    0,    0,    0,
  123.     0,    0,   19,   17,   29,   18,   30,   15,    0,    0,
  124.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  125.     0,    0,    0,    0,    0,   11,    9,    0,   40,   41,
  126.    42,   43,    0,    0,    0,    0,    0,    0,    0,    0,
  127.    45,   37,    0,   27,    0,    0,    0,    0,    0,   38,
  128.    28,   33,    0,   47,   46,   34,
  129. };
  130. short yydgoto[] = {                                      14,
  131.    77,   92,   83,   15,   16,   17,   18,   19,   20,   67,
  132.    21,   22,   23,   24,   25,   26,   27,   28,   29,   69,
  133.    30,   31,
  134. };
  135. short yysindex[] = {                                   -237,
  136.   -50,  -49,  -48,  -47,  -46,  -45,  -44,  -43,  -39,  -38,
  137.   -30,  -22,  -21,    0, -237,    0,    0,    0,    0,    0,
  138.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  139.     0, -239, -238, -236, -235, -234, -233, -232, -230, -228,
  140.  -220, -219, -218, -206,    0, -225,  -11, -223, -222, -221,
  141.  -217, -215,    0,    0,    0,    0,    0,    0,   17,   18,
  142.    20,   -5,    2, -213, -212, -190, -189, -188, -271,   17,
  143.    18,   18,   27,   28,   29,    0,    0,   30,    0,    0,
  144.     0,    0, -193, -271, -182, -180,   17,   17, -179, -178,
  145.     0,    0, -193,    0, -177, -176, -175,   42,   43,    0,
  146.     0,    0, -172,    0,    0,    0,
  147. };
  148. short yyrindex[] = {                                     86,
  149.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  150.     0,    0,    0,    0,   87,    0,    0,    0,    0,    0,
  151.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  152.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  153.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  154.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  155.     0,    0,    0,    0,    0,    0,   16,    0,    1,    0,
  156.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  157.     0,    0,   31,    1,   46,    0,    0,    0,    0,    0,
  158.     0,    0,   31,    0,   61,   76,    0,    0,    0,    0,
  159.     0,    0,   91,    0,    0,    0,
  160. };
  161. short yygindex[] = {                                      0,
  162.     0,   -4,    4,    0,   75,    0,    0,    0,    0,  -70,
  163.     0,    0,    0,    0,    0,    0,    0,    0,    0,  -65,
  164.     0,    0,
  165. };
  166. #define YYTABLESIZE 363
  167. short yytable[] = {                                      84,
  168.    39,   79,   80,   81,   82,   85,   86,   32,   33,   34,
  169.    35,   36,   37,   38,   39,   10,   96,   97,   40,   41,
  170.     1,    2,    3,    4,    5,    6,    7,   42,    8,    9,
  171.    44,   10,   11,   12,   13,   43,   44,   46,   47,   54,
  172.    48,   49,   50,   51,   52,   25,   53,   55,   56,   57,
  173.    58,   59,   60,   61,   62,   63,   66,   68,   71,   64,
  174.    26,   65,   70,   73,   74,   72,   75,   76,   78,   87,
  175.    88,   91,   89,   90,   94,   31,   95,   98,   99,  101,
  176.   102,  103,  104,  105,  106,    1,    2,   93,  100,   45,
  177.    32,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  178.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  179.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  180.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  181.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  182.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  183.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  184.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  185.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  186.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  187.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  188.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  189.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  190.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  191.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  192.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  193.     0,    0,    0,    0,    0,    0,    0,    0,   39,   39,
  194.    39,   39,   39,   39,   39,   39,   39,   39,    0,   39,
  195.    39,   39,   39,   10,   10,   10,   10,   10,   10,   10,
  196.     0,   10,   10,    0,   10,   10,   10,   10,   44,   44,
  197.    44,   44,   44,   44,   44,    0,   44,   44,    0,   44,
  198.    44,   44,   44,   25,   25,   25,   25,   25,   25,   25,
  199.     0,   25,   25,    0,   25,   25,   25,   25,   26,   26,
  200.    26,   26,   26,   26,   26,    0,   26,   26,    0,   26,
  201.    26,   26,   26,   31,   31,   31,   31,   31,   31,   31,
  202.     0,   31,   31,    0,   31,   31,   31,   31,   32,   32,
  203.    32,   32,   32,   32,   32,    0,   32,   32,    0,   32,
  204.    32,   32,   32,
  205. };
  206. short yycheck[] = {                                      70,
  207.     0,  273,  274,  275,  276,   71,   72,   58,   58,   58,
  208.    58,   58,   58,   58,   58,    0,   87,   88,   58,   58,
  209.   258,  259,  260,  261,  262,  263,  264,   58,  266,  267,
  210.     0,  269,  270,  271,  272,   58,   58,  277,  277,  268,
  211.   277,  277,  277,  277,  277,    0,  277,  268,  268,  268,
  212.   257,  277,   64,  277,  277,  277,   40,   40,   64,  277,
  213.     0,  277,   43,  277,  277,   64,  257,  257,  257,   43,
  214.    43,  265,   44,   44,  257,    0,  257,  257,  257,  257,
  215.   257,  257,   41,   41,  257,    0,    0,   84,   93,   15,
  216.     0,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  217.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  218.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  219.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  220.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  221.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  222.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  223.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  224.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  225.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  226.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  227.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  228.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  229.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  230.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  231.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  232.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  258,  259,
  233.   260,  261,  262,  263,  264,  265,  266,  267,   -1,  269,
  234.   270,  271,  272,  258,  259,  260,  261,  262,  263,  264,
  235.    -1,  266,  267,   -1,  269,  270,  271,  272,  258,  259,
  236.   260,  261,  262,  263,  264,   -1,  266,  267,   -1,  269,
  237.   270,  271,  272,  258,  259,  260,  261,  262,  263,  264,
  238.    -1,  266,  267,   -1,  269,  270,  271,  272,  258,  259,
  239.   260,  261,  262,  263,  264,   -1,  266,  267,   -1,  269,
  240.   270,  271,  272,  258,  259,  260,  261,  262,  263,  264,
  241.    -1,  266,  267,   -1,  269,  270,  271,  272,  258,  259,
  242.   260,  261,  262,  263,  264,   -1,  266,  267,   -1,  269,
  243.   270,  271,  272,
  244. };
  245. #define YYFINAL 14
  246. #ifndef YYDEBUG
  247. #define YYDEBUG 0
  248. #endif
  249. #define YYMAXTOKEN 277
  250. #if YYDEBUG
  251. char *yyname[] = {
  252. "end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  253. 0,0,0,0,0,0,"'('","')'",0,"'+'","','",0,0,0,0,0,0,0,0,0,0,0,0,0,"':'",0,0,0,0,0,
  254. "'@'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  255. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  256. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  257. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  258. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"INTEGER",
  259. "A_DUNGEON","BRANCH","CHBRANCH","LEVEL","RNDLEVEL","CHLEVEL","RNDCHLEVEL",
  260. "UP_OR_DOWN","PROTOFILE","DESCRIPTION","DESCRIPTOR","LEVELDESC","ALIGNMENT",
  261. "LEVALIGN","ENTRY","STAIR","NO_UP","NO_DOWN","PORTAL","STRING",
  262. };
  263. char *yyrule[] = {
  264. "$accept : file",
  265. "file :",
  266. "file : dungeons",
  267. "dungeons : dungeon",
  268. "dungeons : dungeons dungeon",
  269. "dungeon : dungeonline",
  270. "dungeon : dungeondesc",
  271. "dungeon : branches",
  272. "dungeon : levels",
  273. "dungeonline : A_DUNGEON ':' STRING STRING rcouple optional_int",
  274. "optional_int :",
  275. "optional_int : INTEGER",
  276. "dungeondesc : entry",
  277. "dungeondesc : descriptions",
  278. "dungeondesc : prototype",
  279. "entry : ENTRY ':' INTEGER",
  280. "descriptions : desc",
  281. "desc : DESCRIPTION ':' DESCRIPTOR",
  282. "desc : ALIGNMENT ':' DESCRIPTOR",
  283. "prototype : PROTOFILE ':' STRING",
  284. "levels : level1",
  285. "levels : level2",
  286. "levels : levdesc",
  287. "levels : chlevel1",
  288. "levels : chlevel2",
  289. "level1 : LEVEL ':' STRING STRING '@' acouple",
  290. "level1 : RNDLEVEL ':' STRING STRING '@' acouple INTEGER",
  291. "level2 : LEVEL ':' STRING STRING '@' acouple INTEGER",
  292. "level2 : RNDLEVEL ':' STRING STRING '@' acouple INTEGER INTEGER",
  293. "levdesc : LEVELDESC ':' DESCRIPTOR",
  294. "levdesc : LEVALIGN ':' DESCRIPTOR",
  295. "chlevel1 : CHLEVEL ':' STRING STRING STRING '+' rcouple",
  296. "chlevel1 : RNDCHLEVEL ':' STRING STRING STRING '+' rcouple INTEGER",
  297. "chlevel2 : CHLEVEL ':' STRING STRING STRING '+' rcouple INTEGER",
  298. "chlevel2 : RNDCHLEVEL ':' STRING STRING STRING '+' rcouple INTEGER INTEGER",
  299. "branches : branch",
  300. "branches : chbranch",
  301. "branch : BRANCH ':' STRING '@' acouple branch_type direction",
  302. "chbranch : CHBRANCH ':' STRING STRING '+' rcouple branch_type direction",
  303. "branch_type :",
  304. "branch_type : STAIR",
  305. "branch_type : NO_UP",
  306. "branch_type : NO_DOWN",
  307. "branch_type : PORTAL",
  308. "direction :",
  309. "direction : UP_OR_DOWN",
  310. "acouple : '(' INTEGER ',' INTEGER ')'",
  311. "rcouple : '(' INTEGER ',' INTEGER ')'",
  312. };
  313. #endif
  314. #ifdef YYSTACKSIZE
  315. #undef YYMAXDEPTH
  316. #define YYMAXDEPTH YYSTACKSIZE
  317. #else
  318. #ifdef YYMAXDEPTH
  319. #define YYSTACKSIZE YYMAXDEPTH
  320. #else
  321. #define YYSTACKSIZE 500
  322. #define YYMAXDEPTH 500
  323. #endif
  324. #endif
  325. int yydebug;
  326. int yynerrs;
  327. int yyerrflag;
  328. int yychar;
  329. short *yyssp;
  330. YYSTYPE *yyvsp;
  331. YYSTYPE yyval;
  332. YYSTYPE yylval;
  333. short yyss[YYSTACKSIZE];
  334. YYSTYPE yyvs[YYSTACKSIZE];
  335. #define yystacksize YYSTACKSIZE
  336.  
  337. void
  338. init_dungeon()
  339. {
  340.     if(++n_dgns > MAXDUNGEON) {
  341.         fprintf(stderr, "FATAL - Too many dungeons (limit: %d).\n",
  342.             MAXDUNGEON);
  343.         fprintf(stderr, "To increase the limit edit MAXDUNGEON in global.h\n");
  344.         exit(1);
  345.     }
  346.  
  347.     in_dungeon = 1;
  348.     tmpdungeon[n_dgns].lev.base = 0;
  349.     tmpdungeon[n_dgns].lev.rand = 0;
  350.     tmpdungeon[n_dgns].chance = 100;
  351.     strcpy(tmpdungeon[n_dgns].name, "");
  352.     strcpy(tmpdungeon[n_dgns].protoname, "");
  353.     tmpdungeon[n_dgns].flags = 0;
  354.     tmpdungeon[n_dgns].levels = 0;
  355.     tmpdungeon[n_dgns].branches = 0;
  356.     tmpdungeon[n_dgns].entry_lev = 0;
  357. }
  358.  
  359. void
  360. init_level()
  361. {
  362.     if(++n_levs > LEV_LIMIT) {
  363.  
  364.         yyerror("FATAL - Too many special levels defined.");
  365.         exit(1);
  366.     }
  367.     tmplevel[n_levs].lev.base = 0;
  368.     tmplevel[n_levs].lev.rand = 0;
  369.     tmplevel[n_levs].chance = 100;
  370.     tmplevel[n_levs].rndlevs = 0;
  371.     tmplevel[n_levs].flags = 0;
  372.     strcpy(tmplevel[n_levs].name, "");
  373.     tmplevel[n_levs].chain = -1;
  374. }
  375.  
  376. void
  377. init_branch()
  378. {
  379.     if(++n_brs > BRANCH_LIMIT) {
  380.  
  381.         yyerror("FATAL - Too many special levels defined.");
  382.         exit(1);
  383.     }
  384.     tmpbranch[n_brs].lev.base = 0;
  385.     tmpbranch[n_brs].lev.rand = 0;
  386.     strcpy(tmpbranch[n_brs].name, "");
  387.     tmpbranch[n_brs].chain = -1;
  388. }
  389.  
  390. int
  391. getchain(s)
  392.     char    *s;
  393. {
  394.     int i;
  395.  
  396.     if(strlen(s)) {
  397.  
  398.         for(i = n_levs - tmpdungeon[n_dgns].levels + 1; i <= n_levs; i++)
  399.         if(!strcmp(tmplevel[i].name, s)) return i;
  400.  
  401.         yyerror("Can't locate the specified chain level.");
  402.         return(-2);
  403.     }
  404.     return(-1);
  405. }
  406.  
  407. /*
  408.  *    Consistancy checking routines:
  409.  *
  410.  *    - A dungeon must have a unique name.
  411.  *    - A dungeon must have a originating "branch" command
  412.  *      (except, of course, for the first dungeon).
  413.  *    - A dungeon must have a proper depth (at least (1, 0)).
  414.  */
  415.  
  416. int
  417. check_dungeon()
  418. {
  419.     int i;
  420.  
  421.     for(i = 0; i < n_dgns; i++)
  422.         if(!strcmp(tmpdungeon[i].name, tmpdungeon[n_dgns].name)) {
  423.         yyerror("Duplicate dungeon name.");
  424.         return(0);
  425.         }
  426.  
  427.     if(n_dgns)
  428.       for(i = 0; i < n_brs - tmpdungeon[n_dgns].branches; i++) {
  429.         if(!strcmp(tmpbranch[i].name, tmpdungeon[n_dgns].name)) break;
  430.  
  431.         if(i >= n_brs - tmpdungeon[n_dgns].branches) {
  432.         yyerror("Dungeon cannot be reached.");
  433.         return(0);
  434.         }
  435.       }
  436.  
  437.     if(tmpdungeon[n_dgns].lev.base <= 0 ||
  438.        tmpdungeon[n_dgns].lev.rand < 0) {
  439.         yyerror("Invalid dungeon depth specified.");
  440.         return(0);
  441.     }
  442.     return(1);    /* OK */
  443. }
  444.  
  445. /*
  446.  *    - A level must have a unique level name.
  447.  *    - If chained, the level used as reference for the chain
  448.  *      must be in this dungeon, must be previously defined, and
  449.  *      the level chained from must be "non-probabalistic" (ie.
  450.  *      have a 100% chance of existing).
  451.  */
  452.  
  453. int
  454. check_level()
  455. {
  456.     int i;
  457.  
  458.     if(!in_dungeon) {
  459.         yyerror("Level defined outside of dungeon.");
  460.         return(0);
  461.     }
  462.  
  463.     for(i = 0; i < n_levs; i++)
  464.         if(!strcmp(tmplevel[i].name, tmplevel[n_levs].name)) {
  465.         yyerror("Duplicate level name.");
  466.         return(0);
  467.         }
  468.  
  469.     if(tmplevel[i].chain == -2) {
  470.         yyerror("Invaild level chain reference.");
  471.         return(0);
  472.     } else if(tmplevel[i].chain != -1) {    /* there is a chain */
  473.         if(tmplevel[tmpbranch[i].chain].chance != 100) {
  474.         yyerror("Level cannot chain from a probabalistic level.");
  475.         return(0);
  476.         } else if(tmplevel[i].chain == n_levs) {
  477.         yyerror("A level cannot chain to itself!");
  478.         return(0);
  479.         }
  480.     }
  481.     return(1);    /* OK */
  482. }
  483.  
  484. /*
  485.  *    - A branch may not branch backwards - to avoid branch loops.
  486.  *    - A branch name must be unique.
  487.  *      (ie. You can only have one entry point to each dungeon).
  488.  *    - If chained, the level used as reference for the chain
  489.  *      must be in this dungeon, must be previously defined, and
  490.  *      the level chained from must be "non-probabalistic" (ie.
  491.  *      have a 100% chance of existing).
  492.  */
  493.  
  494. int
  495. check_branch()
  496. {
  497.     int i;
  498.  
  499.     if(!in_dungeon) {
  500.         yyerror("Branch defined outside of dungeon.");
  501.         return(0);
  502.     }
  503.  
  504.     for(i = 0; i < n_dgns; i++)
  505.         if(!strcmp(tmpdungeon[i].name, tmpbranch[n_brs].name)) {
  506.  
  507.         yyerror("Reverse branching not allowed.");
  508.         return(0);
  509.         }
  510.  
  511.     if(tmpbranch[i].chain == -2) {
  512.  
  513.         yyerror("Invaild branch chain reference.");
  514.         return(0);
  515.     } else if(tmpbranch[i].chain != -1) {    /* it is chained */
  516.  
  517.         if(tmplevel[tmpbranch[i].chain].chance != 100) {
  518.         yyerror("Branch cannot chain from a probabalistic level.");
  519.         return(0);
  520.         }
  521.     }
  522.     return(1);    /* OK */
  523. }
  524.  
  525. /*
  526.  *    Output the dungon definition into a file.
  527.  *
  528.  *    The file will have the following format:
  529.  *
  530.  *    [ number of dungeons ]
  531.  *    [ first dungeon struct ]
  532.  *    [ levels for the first dungeon ]
  533.  *      ...
  534.  *    [ branches for the first dungeon ]
  535.  *      ...
  536.  *    [ second dungeon struct ]
  537.  *      ...
  538.  */
  539.  
  540. void
  541. output_dgn()
  542. {
  543.     int    nd, cl = 0, nl = 0,
  544.             cb = 0, nb = 0;
  545.  
  546.     if(++n_dgns <= 0) {
  547.  
  548.         yyerror("FATAL - no dungeons were defined.");
  549.         exit(1);
  550.     }
  551.  
  552.     fwrite((char *)(&n_dgns), sizeof(int), 1, stdout);
  553.     for(nd = 0; nd < n_dgns; nd++) {
  554.  
  555.         fwrite((char *)&tmpdungeon[nd], sizeof(struct tmpdungeon), 1,
  556.                                 stdout);
  557.  
  558.         nl += tmpdungeon[nd].levels;
  559.         for(; cl < nl; cl++)
  560.         fwrite((char *)&tmplevel[cl], sizeof(struct tmplevel), 1,
  561.                                 stdout);
  562.  
  563.         nb += tmpdungeon[nd].branches;
  564.         for(; cb < nb; cb++)
  565.         fwrite((char *)&tmpbranch[cb], sizeof(struct tmpbranch), 1,
  566.                                 stdout);
  567.     }
  568. }
  569. #define YYABORT goto yyabort
  570. #define YYREJECT goto yyabort
  571. #define YYACCEPT goto yyaccept
  572. #define YYERROR goto yyerrlab
  573. int
  574. yyparse()
  575. {
  576.     register int yym, yyn, yystate;
  577. #if YYDEBUG
  578.     register char *yys;
  579.     extern char *getenv();
  580.  
  581.     if ((yys = getenv("YYDEBUG")) != 0)
  582.     {
  583.         yyn = *yys;
  584.         if (yyn >= '0' && yyn <= '9')
  585.             yydebug = yyn - '0';
  586.     }
  587. #endif
  588.  
  589.     yynerrs = 0;
  590.     yyerrflag = 0;
  591.     yychar = (-1);
  592.  
  593.     yyssp = yyss;
  594.     yyvsp = yyvs;
  595.     *yyssp = yystate = 0;
  596.  
  597. yyloop:
  598.     if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
  599.     if (yychar < 0)
  600.     {
  601.         if ((yychar = yylex()) < 0) yychar = 0;
  602. #if YYDEBUG
  603.         if (yydebug)
  604.         {
  605.             yys = 0;
  606.             if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
  607.             if (!yys) yys = "illegal-symbol";
  608.             printf("%sdebug: state %d, reading %d (%s)\n",
  609.                     YYPREFIX, yystate, yychar, yys);
  610.         }
  611. #endif
  612.     }
  613.     if ((yyn = yysindex[yystate]) != 0 && (yyn += yychar) >= 0 &&
  614.             yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
  615.     {
  616. #if YYDEBUG
  617.         if (yydebug)
  618.             printf("%sdebug: state %d, shifting to state %d\n",
  619.                     YYPREFIX, yystate, yytable[yyn]);
  620. #endif
  621.         if (yyssp >= yyss + yystacksize - 1)
  622.         {
  623.             goto yyoverflow;
  624.         }
  625.         *++yyssp = yystate = yytable[yyn];
  626.         *++yyvsp = yylval;
  627.         yychar = (-1);
  628.         if (yyerrflag > 0)  --yyerrflag;
  629.         goto yyloop;
  630.     }
  631.     if ((yyn = yyrindex[yystate]) != 0 && (yyn += yychar) >= 0 &&
  632.             yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
  633.     {
  634.         yyn = yytable[yyn];
  635.         goto yyreduce;
  636.     }
  637.     if (yyerrflag) goto yyinrecovery;
  638. #ifdef lint
  639.     goto yynewerror;
  640. #endif
  641. yynewerror:
  642.     yyerror("syntax error");
  643. #ifdef lint
  644.     goto yyerrlab;
  645. #endif
  646. yyerrlab:
  647.     ++yynerrs;
  648. yyinrecovery:
  649.     if (yyerrflag < 3)
  650.     {
  651.         yyerrflag = 3;
  652.         for (;;)
  653.         {
  654.             if ((yyn = yysindex[*yyssp]) != 0 && (yyn += YYERRCODE) >= 0 &&
  655.                     yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
  656.             {
  657. #if YYDEBUG
  658.                 if (yydebug)
  659.                     printf("%sdebug: state %d, error recovery shifting\
  660.  to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
  661. #endif
  662.                 if (yyssp >= yyss + yystacksize - 1)
  663.                 {
  664.                     goto yyoverflow;
  665.                 }
  666.                 *++yyssp = yystate = yytable[yyn];
  667.                 *++yyvsp = yylval;
  668.                 goto yyloop;
  669.             }
  670.             else
  671.             {
  672. #if YYDEBUG
  673.                 if (yydebug)
  674.                     printf("%sdebug: error recovery discarding state %d\n",
  675.                             YYPREFIX, *yyssp);
  676. #endif
  677.                 if (yyssp <= yyss) goto yyabort;
  678.                 --yyssp;
  679.                 --yyvsp;
  680.             }
  681.         }
  682.     }
  683.     else
  684.     {
  685.         if (yychar == 0) goto yyabort;
  686. #if YYDEBUG
  687.         if (yydebug)
  688.         {
  689.             yys = 0;
  690.             if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
  691.             if (!yys) yys = "illegal-symbol";
  692.             printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
  693.                     YYPREFIX, yystate, yychar, yys);
  694.         }
  695. #endif
  696.         yychar = (-1);
  697.         goto yyloop;
  698.     }
  699. yyreduce:
  700. #if YYDEBUG
  701.     if (yydebug)
  702.         printf("%sdebug: state %d, reducing by rule %d (%s)\n",
  703.                 YYPREFIX, yystate, yyn, yyrule[yyn]);
  704. #endif
  705.     yym = yylen[yyn];
  706.     yyval = yyvsp[1-yym];
  707.     switch (yyn)
  708.     {
  709. case 2:
  710. {
  711.             output_dgn();
  712.           }
  713. break;
  714. case 9:
  715. {
  716.             init_dungeon();
  717.             strcpy(tmpdungeon[n_dgns].name, yyvsp[-3].str);
  718.             if (!strcmp(yyvsp[-2].str, "none"))
  719.                 tmpdungeon[n_levs].boneschar = '\0';
  720.             else if (yyvsp[-2].str[1])
  721.                 yyerror("Bones marker must be a single char, or \"none\"!");
  722.             else
  723.                 tmpdungeon[n_dgns].boneschar = yyvsp[-2].str[0];
  724.             tmpdungeon[n_dgns].lev.base = couple.base;
  725.             tmpdungeon[n_dgns].lev.rand = couple.rand;
  726.             tmpdungeon[n_dgns].chance = yyvsp[0].i;
  727.           }
  728. break;
  729. case 10:
  730. {
  731.             yyval.i = 0;
  732.           }
  733. break;
  734. case 11:
  735. {
  736.             yyval.i = yyvsp[0].i;
  737.           }
  738. break;
  739. case 15:
  740. {
  741.             tmpdungeon[n_dgns].entry_lev = yyvsp[0].i;
  742.           }
  743. break;
  744. case 17:
  745. {
  746.             if(yyvsp[0].i <= TOWN || yyvsp[0].i >= D_ALIGN_CHAOTIC)
  747.                 yyerror("Illegal description - ignoring!");
  748.             else
  749.                 tmpdungeon[n_dgns].flags |= yyvsp[0].i ;
  750.           }
  751. break;
  752. case 18:
  753. {
  754.             if(yyvsp[0].i && yyvsp[0].i < D_ALIGN_CHAOTIC)
  755.                 yyerror("Illegal alignment - ignoring!");
  756.             else
  757.                 tmpdungeon[n_dgns].flags |= yyvsp[0].i ;
  758.           }
  759. break;
  760. case 19:
  761. {
  762.             strcpy(tmpdungeon[n_dgns].protoname, yyvsp[0].str);
  763.           }
  764. break;
  765. case 25:
  766. {
  767.             init_level();
  768.             strcpy(tmplevel[n_levs].name, yyvsp[-3].str);
  769.             if (!strcmp(yyvsp[-2].str, "none"))
  770.                 tmplevel[n_levs].boneschar = '\0';
  771.             else if (yyvsp[-2].str[1])
  772.                 yyerror("Bones marker must be a single char, or \"none\"!");
  773.             else
  774.                 tmplevel[n_levs].boneschar = yyvsp[-2].str[0];
  775.             tmplevel[n_levs].lev.base = couple.base;
  776.             tmplevel[n_levs].lev.rand = couple.rand;
  777.             tmpdungeon[n_dgns].levels++;
  778.           }
  779. break;
  780. case 26:
  781. {
  782.             init_level();
  783.             strcpy(tmplevel[n_levs].name, yyvsp[-4].str);
  784.             if (!strcmp(yyvsp[-3].str, "none"))
  785.                 tmplevel[n_levs].boneschar = '\0';
  786.             else if (yyvsp[-3].str[1])
  787.                 yyerror("Bones marker must be a single char, or \"none\"!");
  788.             else
  789.                 tmplevel[n_levs].boneschar = yyvsp[-3].str[0];
  790.             tmplevel[n_levs].lev.base = couple.base;
  791.             tmplevel[n_levs].lev.rand = couple.rand;
  792.             tmplevel[n_levs].rndlevs = yyvsp[0].i;
  793.             tmpdungeon[n_dgns].levels++;
  794.           }
  795. break;
  796. case 27:
  797. {
  798.             init_level();
  799.             strcpy(tmplevel[n_levs].name, yyvsp[-4].str);
  800.             if (!strcmp(yyvsp[-3].str, "none"))
  801.                 tmplevel[n_levs].boneschar = '\0';
  802.             else if (yyvsp[-3].str[1])
  803.                 yyerror("Bones marker must be a single char, or \"none\"!");
  804.             else
  805.                 tmplevel[n_levs].boneschar = yyvsp[-3].str[0];
  806.             tmplevel[n_levs].lev.base = couple.base;
  807.             tmplevel[n_levs].lev.rand = couple.rand;
  808.             tmplevel[n_levs].chance = yyvsp[0].i;
  809.             tmpdungeon[n_dgns].levels++;
  810.           }
  811. break;
  812. case 28:
  813. {
  814.             init_level();
  815.             strcpy(tmplevel[n_levs].name, yyvsp[-5].str);
  816.             if (!strcmp(yyvsp[-4].str, "none"))
  817.                 tmplevel[n_levs].boneschar = '\0';
  818.             else if (yyvsp[-4].str[1])
  819.                 yyerror("Bones marker must be a single char, or \"none\"!");
  820.             else
  821.                 tmplevel[n_levs].boneschar = yyvsp[-4].str[0];
  822.             tmplevel[n_levs].lev.base = couple.base;
  823.             tmplevel[n_levs].lev.rand = couple.rand;
  824.             tmplevel[n_levs].chance = yyvsp[-1].i;
  825.             tmplevel[n_levs].rndlevs = yyvsp[0].i;
  826.             tmpdungeon[n_dgns].levels++;
  827.           }
  828. break;
  829. case 29:
  830. {
  831.             if(yyvsp[0].i >= D_ALIGN_CHAOTIC)
  832.                 yyerror("Illegal description - ignoring!");
  833.             else
  834.                 tmplevel[n_levs].flags |= yyvsp[0].i ;
  835.           }
  836. break;
  837. case 30:
  838. {
  839.             if(yyvsp[0].i && yyvsp[0].i < D_ALIGN_CHAOTIC)
  840.                 yyerror("Illegal alignment - ignoring!");
  841.             else
  842.                 tmplevel[n_levs].flags |= yyvsp[0].i ;
  843.           }
  844. break;
  845. case 31:
  846. {
  847.             init_level();
  848.             strcpy(tmplevel[n_levs].name, yyvsp[-4].str);
  849.             if (!strcmp(yyvsp[-3].str, "none"))
  850.                 tmplevel[n_levs].boneschar = '\0';
  851.             else if (yyvsp[-3].str[1])
  852.                 yyerror("Bones marker must be a single char, or \"none\"!");
  853.             else
  854.                 tmplevel[n_levs].boneschar = yyvsp[-3].str[0];
  855.             tmplevel[n_levs].chain = getchain(yyvsp[-2].str);
  856.             tmplevel[n_levs].lev.base = couple.base;
  857.             tmplevel[n_levs].lev.rand = couple.rand;
  858.             if(!check_level()) n_levs--;
  859.             else tmpdungeon[n_dgns].levels++;
  860.           }
  861. break;
  862. case 32:
  863. {
  864.             init_level();
  865.             strcpy(tmplevel[n_levs].name, yyvsp[-5].str);
  866.             if (!strcmp(yyvsp[-4].str, "none"))
  867.                 tmplevel[n_levs].boneschar = '\0';
  868.             else if (yyvsp[-4].str[1])
  869.                 yyerror("Bones marker must be a single char, or \"none\"!");
  870.             else
  871.                 tmplevel[n_levs].boneschar = yyvsp[-4].str[0];
  872.             tmplevel[n_levs].chain = getchain(yyvsp[-3].str);
  873.             tmplevel[n_levs].lev.base = couple.base;
  874.             tmplevel[n_levs].lev.rand = couple.rand;
  875.             tmplevel[n_levs].rndlevs = yyvsp[0].i;
  876.             if(!check_level()) n_levs--;
  877.             else tmpdungeon[n_dgns].levels++;
  878.           }
  879. break;
  880. case 33:
  881. {
  882.             init_level();
  883.             strcpy(tmplevel[n_levs].name, yyvsp[-5].str);
  884.             if (!strcmp(yyvsp[-4].str, "none"))
  885.                 tmplevel[n_levs].boneschar = '\0';
  886.             else if (yyvsp[-4].str[1])
  887.                 yyerror("Bones marker must be a single char, or \"none\"!");
  888.             else
  889.                 tmplevel[n_levs].boneschar = yyvsp[-4].str[0];
  890.             tmplevel[n_levs].chain = getchain(yyvsp[-3].str);
  891.             tmplevel[n_levs].lev.base = couple.base;
  892.             tmplevel[n_levs].lev.rand = couple.rand;
  893.             tmplevel[n_levs].chance = yyvsp[0].i;
  894.             if(!check_level()) n_levs--;
  895.             else tmpdungeon[n_dgns].levels++;
  896.           }
  897. break;
  898. case 34:
  899. {
  900.             init_level();
  901.             strcpy(tmplevel[n_levs].name, yyvsp[-6].str);
  902.             if (!strcmp(yyvsp[-5].str, "none"))
  903.                 tmplevel[n_levs].boneschar = '\0';
  904.             else if (yyvsp[-5].str[1])
  905.                 yyerror("Bones marker must be a single char, or \"none\"!");
  906.             else
  907.                 tmplevel[n_levs].boneschar = yyvsp[-5].str[0];
  908.             tmplevel[n_levs].chain = getchain(yyvsp[-4].str);
  909.             tmplevel[n_levs].lev.base = couple.base;
  910.             tmplevel[n_levs].lev.rand = couple.rand;
  911.             tmplevel[n_levs].chance = yyvsp[-1].i;
  912.             tmplevel[n_levs].rndlevs = yyvsp[0].i;
  913.             if(!check_level()) n_levs--;
  914.             else tmpdungeon[n_dgns].levels++;
  915.           }
  916. break;
  917. case 37:
  918. {
  919.             init_branch();
  920.             strcpy(tmpbranch[n_brs].name, yyvsp[-4].str);
  921.             tmpbranch[n_brs].lev.base = couple.base;
  922.             tmpbranch[n_brs].lev.rand = couple.rand;
  923.             tmpbranch[n_brs].type = yyvsp[-1].i;
  924.             tmpbranch[n_brs].up = yyvsp[0].i;
  925.             if(!check_branch()) n_brs--;
  926.             else tmpdungeon[n_dgns].branches++;
  927.           }
  928. break;
  929. case 38:
  930. {
  931.             init_branch();
  932.             strcpy(tmpbranch[n_brs].name, yyvsp[-5].str);
  933.             tmpbranch[n_brs].chain = getchain(yyvsp[-4].str);
  934.             tmpbranch[n_brs].lev.base = couple.base;
  935.             tmpbranch[n_brs].lev.rand = couple.rand;
  936.             tmpbranch[n_brs].type = yyvsp[-1].i;
  937.             tmpbranch[n_brs].up = yyvsp[0].i;
  938.             if(!check_branch()) n_brs--;
  939.             else tmpdungeon[n_dgns].branches++;
  940.           }
  941. break;
  942. case 39:
  943. {
  944.             yyval.i = TBR_STAIR;    /* two way stair */
  945.           }
  946. break;
  947. case 40:
  948. {
  949.             yyval.i = TBR_STAIR;    /* two way stair */
  950.           }
  951. break;
  952. case 41:
  953. {
  954.             yyval.i = TBR_NO_UP;    /* no up staircase */
  955.           }
  956. break;
  957. case 42:
  958. {
  959.             yyval.i = TBR_NO_DOWN;    /* no down staircase */
  960.           }
  961. break;
  962. case 43:
  963. {
  964.             yyval.i = TBR_PORTAL;    /* portal connection */
  965.           }
  966. break;
  967. case 44:
  968. {
  969.             yyval.i = 0;    /* defaults to down */
  970.           }
  971. break;
  972. case 45:
  973. {
  974.             yyval.i = yyvsp[0].i;
  975.           }
  976. break;
  977. case 46:
  978. {
  979.             if (yyvsp[-3].i < -MAXLEVEL || yyvsp[-3].i > MAXLEVEL) {
  980.                 yyerror("Abs base out of dlevel range - zeroing!");
  981.                 couple.base = couple.rand = 0;
  982.             } else if (yyvsp[-1].i < -1 ||
  983.                 ((yyvsp[-3].i < 0) ? (MAXLEVEL + yyvsp[-3].i + yyvsp[-1].i + 1) > MAXLEVEL :
  984.                     (yyvsp[-3].i + yyvsp[-1].i) > MAXLEVEL)) {
  985.                 yyerror("Abs range out of dlevel range - zeroing!");
  986.                 couple.base = couple.rand = 0;
  987.             } else {
  988.                 couple.base = yyvsp[-3].i;
  989.                 couple.rand = yyvsp[-1].i;
  990.             }
  991.           }
  992. break;
  993. case 47:
  994. {
  995.             if (yyvsp[-3].i < -MAXLEVEL || yyvsp[-3].i > MAXLEVEL) {
  996.                 yyerror("Rel base out of dlevel range - zeroing!");
  997.                 couple.base = couple.rand = 0;
  998.             } else {
  999.                 couple.base = yyvsp[-3].i;
  1000.                 couple.rand = yyvsp[-1].i;
  1001.             }
  1002.           }
  1003. break;
  1004.     }
  1005.     yyssp -= yym;
  1006.     yystate = *yyssp;
  1007.     yyvsp -= yym;
  1008.     yym = yylhs[yyn];
  1009.     if (yystate == 0 && yym == 0)
  1010.     {
  1011. #if YYDEBUG
  1012.         if (yydebug)
  1013.             printf("%sdebug: after reduction, shifting from state 0 to\
  1014.  state %d\n", YYPREFIX, YYFINAL);
  1015. #endif
  1016.         yystate = YYFINAL;
  1017.         *++yyssp = YYFINAL;
  1018.         *++yyvsp = yyval;
  1019.         if (yychar < 0)
  1020.         {
  1021.             if ((yychar = yylex()) < 0) yychar = 0;
  1022. #if YYDEBUG
  1023.             if (yydebug)
  1024.             {
  1025.                 yys = 0;
  1026.                 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
  1027.                 if (!yys) yys = "illegal-symbol";
  1028.                 printf("%sdebug: state %d, reading %d (%s)\n",
  1029.                         YYPREFIX, YYFINAL, yychar, yys);
  1030.             }
  1031. #endif
  1032.         }
  1033.         if (yychar == 0) goto yyaccept;
  1034.         goto yyloop;
  1035.     }
  1036.     if ((yyn = yygindex[yym]) != 0 && (yyn += yystate) >= 0 &&
  1037.             yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
  1038.         yystate = yytable[yyn];
  1039.     else
  1040.         yystate = yydgoto[yym];
  1041. #if YYDEBUG
  1042.     if (yydebug)
  1043.         printf("%sdebug: after reduction, shifting from state %d \
  1044. to state %d\n", YYPREFIX, *yyssp, yystate);
  1045. #endif
  1046.     if (yyssp >= yyss + yystacksize - 1)
  1047.     {
  1048.         goto yyoverflow;
  1049.     }
  1050.     *++yyssp = yystate;
  1051.     *++yyvsp = yyval;
  1052.     goto yyloop;
  1053. yyoverflow:
  1054.     yyerror("yacc stack overflow");
  1055. yyabort:
  1056.     return (1);
  1057. yyaccept:
  1058.     return (0);
  1059. }
  1060.